import { useEffect } from 'react'
import { useNavigate, useLocation } from 'react-router-dom'
import useGetUserInfo from './useGetUserInfo'
import { isLoginOrRegister, isNoNeedUserInfo } from '../router'
import { MANAGE_INDEX_PATHNAME, LOGIN_PATHNAME } from '../router'

function useNavPage(waitingUserData: boolean) {
    const { username, nickname } = useGetUserInfo()
    const nav = useNavigate()
    const { pathname } = useLocation()

    useEffect(() => {
        if (waitingUserData) return
        if (username) {
            if (isLoginOrRegister(pathname)) {
                nav(MANAGE_INDEX_PATHNAME)
            }
            return
        }
        // 未登录
        if (isNoNeedUserInfo(pathname)) {
            return
        } else {
            nav(LOGIN_PATHNAME)
        }
    }, [username, pathname, waitingUserData])
}

export default useNavPage
